﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PicStore.BLL.Impl;
using PicStore.Common.Ioc;
using PicStore.Common.Log;
using PicStore.UploaderInterface;

namespace PicStore.Server
{
    public class FileReceiver : MarshalByRefObject, IUploader
    {
        private ILog Log = ObjectFactory.GetInstance<ILog>();
        public bool SendFile(byte[] bytData, int userId, string pictureName, DateTime takePhotoDate, string place, string sex,
                      string item, string color, string style, string length,
                      string collar, string front, string sleeve, string sweaterTexture, string type)
        {
            bool result = false;
            try
            {
                string dateFolder = takePhotoDate.ToString("yyyy-MM-dd");
                // save the file.
                string physicalPath = string.Format("{0}{1}", ConfigurationManager.AppSettings["UploadFolder"], dateFolder);

                if (Directory.Exists(physicalPath) == false)
                {
                    Directory.CreateDirectory(physicalPath);
                }
                string fileName = string.Format(@"{0}\{1}", physicalPath, pictureName);

                // Open the stream and write to it. 
                using (FileStream fs = File.OpenWrite(fileName))
                {
                    // Add some information to the file.
                    fs.Write(bytData, 0, bytData.Length);
                }

                // create the record in the database.
                var pictureService = new PictureService();
                pictureService.CreatePicture(userId, fileName, Path.GetFileName(fileName), string.Format(@"{0}\{1}", dateFolder, pictureName), takePhotoDate, place, sex, item, color, style, length, collar,
                        front, sleeve, sweaterTexture, type);

                result = true;
            }
            catch (Exception)
            {
                result = false;
                throw;
            }

            return result;
        }

        /// <summary>
        /// 仅保留存储上传文件的逻辑。
        /// </summary>
        public bool SendFile(byte[] bytData, string pictureName, DateTime takePhotoDate, out string finalServerFileName, out string relativePathName)
        {
            bool result = false;
            finalServerFileName = string.Empty;
            relativePathName = string.Empty;
            try
            {
                string dateFolder = takePhotoDate.ToString("yyyy-MM-dd");
                // save the file.
                string physicalPath = string.Format("{0}{1}", ConfigurationManager.AppSettings["UploadFolder"], dateFolder);
                
                if (Directory.Exists(physicalPath) == false)
                {
                    Directory.CreateDirectory(physicalPath);
                }
                string fileName = string.Format(@"{0}\{1}", physicalPath, pictureName);

                // Open the stream and write to it. 
                using (FileStream fs = File.OpenWrite(fileName))
                {
                    // Add some information to the file.
                    fs.Write(bytData, 0, bytData.Length);
                }

                relativePathName = string.Format(@"{0}\{1}", dateFolder, pictureName);
                finalServerFileName = fileName;
                result = true;
            }
            catch (Exception exception)
            {
                result = false;
                Log.Severe(string.Format("save the file {0} failed", pictureName), exception);
            }

            return result;
        }


        public bool IsServerActive()
        {
            return true;
        }
    }
}
